package com.example.upimageadapter;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.upimageadapter.adapter.UpImagesListAdapter;
import com.example.upimageadapter.other.GlideEngine;
import com.example.upimageadapter.other.MyItemTouchHelperCallback;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements MyItemTouchHelperCallback.OnStartDragListener {

    private RecyclerView recyclerView;
    private ItemTouchHelper mItemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        UpImagesListAdapter upImagesListAdapter = new UpImagesListAdapter(this, this, 3);
        recyclerView.setAdapter(upImagesListAdapter);
        mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelperCallback(upImagesListAdapter));
        mItemTouchHelper.attachToRecyclerView(recyclerView);

        upImagesListAdapter.setAddClickListener(new UpImagesListAdapter.AddClickListener() {
            @Override
            public void click(View view) {
                int maxCount = upImagesListAdapter.getMaxCount() - upImagesListAdapter.getData().size();
                PictureSelector.create(MainActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .loadImageEngine(new GlideEngine())
                        .maxSelectNum(maxCount)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(List<LocalMedia> result) {
                                List<String> list = new ArrayList<>();
                                for (int i = 0; i < result.size(); i++) {
                                    list.add(result.get(i).getPath());
                                }
//                                upImagesListAdapter.notifyItemRemoved(upImagesListAdapter.getItemCount());
                                upImagesListAdapter.addData(list);
//                                upImagesListAdapter.notifyItemChanged(0, list.size());

                            }

                            @Override
                            public void onCancel() {
                                // onCancel Callback
                            }
                        });
            }
        });
    }

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        //通知ItemTouchHelper开始拖拽
        mItemTouchHelper.startDrag(viewHolder);
    }
}
